# !/usr/bin/env python
# coding=utf-8
# @Time    : 2018/7/16 11:18
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/rpc_dao.git
# @File    : car_raw_info_service.py
# @Software: PyCharm
# @Version: 2.7.5
from idl_output.ttypes import CarRawInfo
from utils import my_decorator
from db import car_raw_info


@my_decorator.handle_exception()
def save_car_raw_info(info):
	thrift_data = CarRawInfo(info)
	car_raw_info.save_car_raw_info(thrift_data.license_no, thrift_data.vin_no, thrift_data.engine_no,
	                               thrift_data.model, thrift_data.brand, thrift_data.register_date,
	                               thrift_data.owner_name, thrift_data.owner_code, thrift_data.owner_mobile,
	                               thrift_data.trans_date, thrift_data.is_trans)


# @my_decorator.handle_exception()
def get_car_raw_info_by_license(license):
	orm_data = car_raw_info.get_car_raw_info_by_license(license)
	if not orm_data:
		return None

	thrift_data = CarRawInfo(license_no=orm_data.licenseNo, vin_no=orm_data.vehicleFrameNo,
	                         engine_no=orm_data.engine_code,
	                         model=orm_data.model, brand=orm_data.brand,
	                         register_date=orm_data.register_date,
	                         trans_date=orm_data.trans_date, is_trans=orm_data.is_trans)
	return thrift_data


@my_decorator.handle_exception()
def get_car_raw_info_by_vin(vin):
	orm_data = car_raw_info.get_car_raw_info_by_vin(vin)
	if not orm_data:
		return None

	thrift_data = CarRawInfo(license_no=orm_data.licenseNo, vin_no=orm_data.vehicleFrameNo,
	                         engine_no=orm_data.engine_code,
	                         model=orm_data.model, brand=orm_data.brand,
	                         register_date=orm_data.register_date,
	                         trans_date=orm_data.trans_date, is_trans=orm_data.is_trans)
	return thrift_data


@my_decorator.handle_exception()
def get_car_owner_info(vin, license):
	orm_data = car_raw_info.get_car_owner_info(license, vin)
	if not orm_data:
		return None

	thrift_data = CarRawInfo(owner_name=orm_data.owner_name, owner_code=orm_data.owner_code,
	                         owner_mobile=orm_data.owner_mobile)
	return thrift_data


@my_decorator.handle_exception()
def update_car_raw_info(info, license):
	thrift_data = CarRawInfo(info)
	car_raw_info.update_car_raw_info_by_license(license, thrift_data.vin_no,
	                                            thrift_data.engine_no,
	                                            thrift_data.model,
	                                            thrift_data.brand,
	                                            thrift_data.register_date,
	                                            thrift_data.trans_date,
	                                            thrift_data.is_trans)


@my_decorator.handle_exception()
def update_car_owner_info(info, license, vin):
	thrift_data = CarRawInfo(info)
	car_raw_info.update_car_owner_info(license, vin, thrift_data.owner_name, thrift_data.owner_code,
	                                   thrift_data.owner_mobile)
